﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LJX.SmartPark.Service.IotInfo.SmartLamp.SmartLampService;

namespace LJX.SmartPark.Service.IotInfo.SmartLamp
{
    public class StatusLogsService : IStatusLogsService
    {
        private readonly IRepository<Domain.IotInfo.SmartLamp.StatusLogs> _StatusLogsService;

        public StatusLogsService(IRepository<Domain.IotInfo.SmartLamp.StatusLogs> StatusLogsService)
        {
            _StatusLogsService = StatusLogsService;
        }
        //状态日志表显示
        public async Task<Response<List<Domain.IotInfo.SmartLamp.StatusLogs>>> GetStatusLogsAsync()
        {
            var StatusLogs = await _StatusLogsService.GetAllListAsync();
            var result = StatusLogs.MapToList<Domain.IotInfo.SmartLamp.StatusLogs>();
            return Response<List<Domain.IotInfo.SmartLamp.StatusLogs>>.Ok(result);
        }
        //状态日志表显示根据设备id查询
        public Response<Domain.IotInfo.SmartLamp.StatusLogs> GetStatusLogsData(Domain.IotInfo.SmartLamp.StatusLogs input)
        {
            var StatusLogs = _StatusLogsService.GetListQuery().FirstOrDefault(m => m.Id == input.DeviceId);
            var result = StatusLogs.MapTo<Domain.IotInfo.SmartLamp.StatusLogs>();
            return Response<Domain.IotInfo.SmartLamp.StatusLogs>.Ok(result);
        }
        //状态日志表添加
        public async Task<Response<bool>> StatusLogsAdd(Domain.IotInfo.SmartLamp.StatusLogs input)
        {
            var result = _StatusLogsService.InsertAsyncById(input.MapTo<Domain.IotInfo.SmartLamp.StatusLogs>());

            return Response<bool>.Ok(result.Result > 0);
        }
        //状态日志表删除
        public async Task<Response<bool>> StatusLogsDelete(Domain.IotInfo.SmartLamp.StatusLogs input)
        {
            var result = await _StatusLogsService.DeleteByIdAsync(input.MapTo<Domain.IotInfo.SmartLamp.StatusLogs>());
            return Response<bool>.Ok(result > 0);
        }
        //状态日志表修改
        public async Task<Response<bool>> StatusLogsUpdate(Domain.IotInfo.SmartLamp.StatusLogs input)
        {
            var result = _StatusLogsService.UpdateAsync(input.MapTo<Domain.IotInfo.SmartLamp.StatusLogs>());

            return Response<bool>.Ok(result.Result > 0);
        }
        //总条数
        public int GetCount()
        {
            return _StatusLogsService.GetCount();
        }

        //状态日志显示分页查询
        public async Task<Response<List<Domain.IotInfo.SmartLamp.StatusLogs>>> GetStatusLogsPage(GetSelectLogData select)
        {
            var datas = await _StatusLogsService.GetAllListAsync();
            var result = datas.MapToList<Domain.IotInfo.SmartLamp.StatusLogs>();
            //设备编号 灯状态 在线状态  创建时间查询
            //设备编号
            if (!string.IsNullOrWhiteSpace(select.EquipmentNum))
            {
                result = result.Where(x => x.EquipmentNum.Contains(select.EquipmentNum)).ToList();
            }
            //灯状态
            if (select.LightStatus!=null)
            {
                result = result.Where(x => x.LightStatus == select.LightStatus).ToList();
            }
            //在线状态
            if (select.Online != null)
            {
                result = result.Where(x => x.Online == select.Online).ToList();
            }
            //创建时间
            if (select.CreateTime != null)
            {
                result = result.Where(x => x.CreateTime == select.CreateTime).ToList();
            }

            var totalCount = result.Count();

            result = result.Skip(((select.pageIndex - 1) * select.pageSize)).Take(select.pageSize).ToList();

            return Response<List<Domain.IotInfo.SmartLamp.StatusLogs>>.Ok(result, totalCount);
        }

        //页数条数model类
        public class GetSelectLogData
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string EquipmentNum { get; set; }
            public bool LightStatus { get; set; }
            public bool Online { get; set; }
            public DateTime CreateTime { get; set; }
        }
    }
}
